package com.caimiao.miao.library.utils.sys;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import java.lang.reflect.Method;
//网络工具
public class NetUtil {

    public static boolean isWifiEnable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) return false;
        NetworkInfo info = cm.getActiveNetworkInfo();
        return !(info == null || !(info.isConnected())) && info.getType() == ConnectivityManager.TYPE_WIFI;
    }

    public static boolean isMobileEnable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) return false;
        NetworkInfo info = cm.getActiveNetworkInfo();
        return !(info == null || !(info.isConnected())) && info.getType() == ConnectivityManager.TYPE_MOBILE;
    }

    public static boolean isMobileEnableReflex(Context context) {
        try {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            Method getMobileDataEnabledMethod = ConnectivityManager.class.getDeclaredMethod("getMobileDataEnabled");
            getMobileDataEnabledMethod.setAccessible(true);
            return (Boolean) getMobileDataEnabledMethod.invoke(cm);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isAllEnable(Context context) {
        return isWifiEnable(context) && isMobileEnableReflex(context);
    }
}
